package gaml.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import gaml.error.GamlException;
import gaml.text.DefaultParserStoreFactory;
import gaml.text.TextNodeBridge;
import gaml.text.YamlParserStore;
import gaml.text.YamlText;
import gaml.yaml.YamlNode;
import gaml.yaml.ahchor.YamlAnchorCodeStore;

import org.junit.Test;


public class MappingNodeParserTest {
	final static String sampleText = "!maptag &mapanch {\n	? > mapkey3\n	: [\n		- > seqval1\n		- > seqval2\n	]\n	? > mapkey4\n	: > mapval4\n	? > mapkey2\n	: > mapval2\n	? > mapkey1\n	: > mapval1\n}";
	
	YamlAnchorCodeStore anchors = new YamlAnchorCodeStore ();
	
	@Test
	public void testParseText () throws GamlException {
		YamlText text = new YamlText ().read (sampleText);

/*		YamlParserStore store = new YamlParserStore (new ScalarNodeParser (anchors));
		store.register (new MappingNodeParser (anchors, store));
		store.register (new AliasNodeParser (anchors));
*/
		YamlParserStore store = DefaultParserStoreFactory.getStore (new YamlAnchorCodeStore ());
		
		assertTrue (store.hasParser (text));

		assertFalse (text.hasTail ());

		YamlNode node = new TextNodeBridge (text, null).parseText (store).getNode ();
		
		assertEquals (4, node.getMappingChildren ().size ());

		for (YamlNode key: node.getMappingChildren ().keySet ()) {
			if ("mapkey4".equals (key.getValue ()))
				assertEquals ("mapval4", node.getMappingChildren ().get (key).getValue ());
			System.out.println (key.getValue () + " = " + node.getMappingChildren ().get (key).getValue ());
		}
	}
}